package com.uzykj.modules.front.service;

import com.uzykj.common.utils.CacheUtils;
import com.uzykj.common.utils.number.NumberUtil;
import com.uzykj.core.persistence.Page;
import com.uzykj.modules.blog.entity.BlogCategory;
import com.uzykj.modules.blog.entity.BlogNote;
import com.uzykj.modules.blog.service.BlogCategoryService;
import com.uzykj.modules.blog.service.BlogNoteService;
import com.uzykj.modules.sys.entity.User;
import com.uzykj.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ghostxbh
 * @date 2020/8/10
 * @description
 */
@Service
public class NoteService {
    private static final int EXPIRE = 24 * 60 * 60;
    @Autowired
    private BlogCategoryService blogCategoryService;

    @Autowired
    private BlogNoteService blogNoteService;

    public Page<BlogNote> search(Page<BlogNote> page, String city, String search, String label, String category, String creator) {
        BlogNote note = new BlogNote();
        if (city != null) {
            note.setCity(city);
            page = blogNoteService.findPage(page, note);
        } else if (label != null) {
            note.setLabels(label);
            page = blogNoteService.findPage(page, note);
        } else if (category != null) {
            BlogCategory blogCategory = new BlogCategory();
            blogCategory.setName(category);
            note.setCategory(blogCategory);
            page = blogNoteService.findPage(page, note);
        } else if (creator != null) {
            User user = UserUtils.getByName(creator);
            if (user != null)
                note.setCreateBy(new User(user.getId()));
            page = blogNoteService.findPage(page, note);
        } else if (search != null) {
            if (NumberUtil.isNumber(search)) {
                note.setCode(search);
                page = blogNoteService.findPage(page, note);
            } else {
                // 缓存key
                String key = UserUtils.CACHE_BLOG_LIST + "_" + search.hashCode();
                List<BlogNote> noteCacheList = (List<BlogNote>) CacheUtils.get(UserUtils.BLOG_CACHE, key);
                if (noteCacheList != null && noteCacheList.size() > 0) {
                    // 物理分页
                    List<BlogNote> pageList = page.page(page.getPageNo(), page.getPageSize(), noteCacheList);
                    page.setCount(noteCacheList.size());
                    page.setList(pageList);
                } else {
                    List<BlogNote> noteList = new ArrayList<BlogNote>();
                    // 类型
                    BlogNote note1 = new BlogNote();
                    BlogCategory category1 = new BlogCategory();
                    category1.setName(search);
                    note1.setCategory(category1);
                    List<BlogNote> noteList1 = blogNoteService.getMostPopular(note1);
                    noteList.addAll(noteList1);

                    // 标签
                    BlogNote note2 = new BlogNote();
                    note2.setLabels(search);
                    List<BlogNote> noteList2 = blogNoteService.getMostPopular(note2);
                    noteList.addAll(noteList2);

                    // 标题
                    BlogNote note3 = new BlogNote();
                    note3.setTitle(search);
                    List<BlogNote> noteList3 = blogNoteService.getMostPopular(note3);
                    noteList.addAll(noteList3);

                    // 创建者
                    BlogNote note4 = new BlogNote();
                    User user4 = new User();
                    user4.setName(search);
                    note4.setCreateBy(user4);
                    List<BlogNote> noteList4 = blogNoteService.getMostPopular(note4);
                    noteList.addAll(noteList4);

                    // 标签
                    BlogNote note5 = new BlogNote();
                    note5.setCity(search);
                    List<BlogNote> noteList5 = blogNoteService.getMostPopular(note5);
                    noteList.addAll(noteList5);

                    // 缓存
                    CacheUtils.put(UserUtils.BLOG_CACHE, key, noteList, EXPIRE);
                    List<BlogNote> pageList = page.page(page.getPageNo(), page.getPageSize(), noteList);
                    page.setCount(noteList.size());
                    page.setList(pageList);
                }
            }
        }
        if (page.getList() == null) page.setList(new ArrayList<BlogNote>(0));
        return page;
    }

    public List<User> getTraveler(){
        return null;
    }
}
